﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
using System.IO;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Bson.Serialization.Options;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Wrappers;


#region 数据实体
/// <summary>
/// 数据实体
/// </summary>
public class Item
{
    [BsonId]
    public ObjectId Id { get; set; }

    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("price")]
    public double Price { get; set; }

    [BsonElement("attributes")]
    public BsonDocument Attri { get; set; }

    //public DateTime CreateTime { get; set; }
}
#endregion

public partial class QueryItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        gvBind();
    }

    protected void gvDbItemInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        gvBind();
    }
    protected void gvBind()
    {

        MongoServer server = MongoServer.Create(); // connect to localhost
        MongoDatabase db = server.GetDatabase("swallow");
        // 获取集合employees
        //MongoCollection collection = db.GetCollection("taobao2");
        var collection = db.GetCollection<Item>("taobao2");

        //MongoCollection<BsonDocument> items = db.GetCollection<BsonDocument>("taobao2");

        //// 遍历结果
        //foreach (BsonDocument ite in items.Find(query))
        //{
        //    // BsonValue有两种取值方式，下面两个都用了一个是AsXXX,一个是ToXXX
        //    Label_Test.Text += ite["name"].AsString + ite["price"].ToString() + " </br>";
        //}

        int intCondition = ddlCondition.SelectedIndex;

        if (txtCondition.Text == "")
        {
            //ds = gtermanage.GetAllGter("tb_gter");
        }
        else
        {
            switch (intCondition)
            {
                case 0:

                    break;
                case 1:

                    var query = Query.EQ("name", txtCondition.Text.ToString() + ".*$" );
                    var cursor = collection.Find(query);

                    //foreach (BsonDocument ite in items.Find(query))
                    //{
                    //    // BsonValue有两种取值方式，下面两个都用了一个是AsXXX,一个是ToXXX
                    //    LabelTest.Text += Convert.ToInt32(txtCondition.Text.ToString()).ToString() + ite["name"].AsString + ite["price"].ToString() + " </br>";
                    //}
                    cursor.SetLimit(50);
                    cursor.SetSortOrder(SortBy.Ascending("price"));

                    var resultList = cursor.ToList();

                    gvDbItemInfo.DataSource = resultList;
                    gvDbItemInfo.DataBind();

                    break;
                case 2:

                    //this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert('建议您使用IE进行浏览，本站对Firebox的支持尚未完善')</script>");

                    query = Query.EQ("price", Convert.ToInt32(txtCondition.Text.ToString()) );
                    cursor = collection.Find(query);

                    //foreach (BsonDocument ite in items.Find(query))
                    //{
                    //    // BsonValue有两种取值方式，下面两个都用了一个是AsXXX,一个是ToXXX
                    //    LabelTest.Text += Convert.ToInt32(txtCondition.Text.ToString()).ToString() + ite["name"].AsString + ite["price"].ToString() + " </br>";
                    //}
                    cursor.SetLimit(50);
                    cursor.SetSortOrder(SortBy.Ascending("name"));

                    resultList = cursor.ToList();

                    gvDbItemInfo.DataSource = resultList;
                    gvDbItemInfo.DataBind();

                    break;
                case 3:

                    break;
            }
        }


    }
    protected string cutstr(string str, int len)
    {
        if (str.Length > len)
        {
            return str.Substring(0, len) + "…";
        }
        else
        {
            return str;
        }
    }
    protected void gvDbItemInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDbItemInfo.PageIndex = e.NewPageIndex;
        gvBind();
    }
}
